#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <iostream>
using namespace std;

class client
{
private:
    string ip;
    int port;
    int client_sockfd;

public:
    client(string ip, int port) : ip(ip), port(port) {}
    ~client() { close(client_sockfd); }
    void run();
};

void client::run()
{
    client_sockfd = socket(AF_INET, SOCK_STREAM, 0);

    sockaddr_in server_socket;
    memset(&server_socket, 0, sizeof(server_socket));
    server_socket.sin_family = AF_INET;
    server_socket.sin_port = htons(port);
    server_socket.sin_addr.s_addr = inet_addr(ip.c_str());

    if (connect(client_sockfd, (struct sockaddr *)&server_socket, sizeof(server_socket)) < 0)
    {
        perror("connect");
        exit(1);
    }
    cout << "连接服务器成功！\n";

    while (1)
    {
        char sendBuf[100];
        memset(sendBuf, 0, sizeof(sendBuf));
        cout << "please enter msg:";
        cin >> sendBuf;
        send(client_sockfd, sendBuf, strlen(sendBuf), 0); // 发送
        if (strcmp(sendBuf, "exit") == 0)
            break;
    }
}

int main()
{
    client cli("127.0.0.1", 8888);
    cli.run();
    return 0;
}